﻿interface JQuery {
    flexslider: any;
}

module Sustainalytics.LRTM {
    
    // LRTM controller
    export class LongRunningTaskManagerController {
        private previousInstances: number;

        constructor(
            private $scope: ng.IScope,
            private $q: ng.IQService,
            private LongRunningTaskManagerService: LongRunningTaskManagerService) {
            
            this.$scope['lrtm'] = this;
            this.$scope['lrtm'].ltrmShow = false;
        }

        // get task instances
        public getTaskInstances(): LongRunningTaskInstance[] {
            var instances = this.LongRunningTaskManagerService.getTaskInstances();

            //$('.flexslider').flexslider({
            //    animation: "slide",
            //    animationLoop: false,
            //    itemWidth: 350,
            //    maxItems: 0,
            //    itemMargin: 5
            //});
            if (this.previousInstances != instances.length) {
                this.$scope['lrtm'].ltrmShow = true;
                this.previousInstances = instances.length;
            }
            return instances;
        }

        // cancel task
        public cancelTask(taskInstance: LongRunningTaskInstance) {
            if (!taskInstance.canCancel())
                return;

            this.LongRunningTaskManagerService.deleteTask(taskInstance.status.id);
        }

        // close task
        public closeTask(taskInstance: LongRunningTaskInstance) {
            if (!taskInstance.canClose())
                return;

            this.LongRunningTaskManagerService.deleteTask(taskInstance.status.id);
        }

        // download
        public downloadTask(taskInstance: LongRunningTaskInstance) {
            if (!taskInstance.canDownload())
                return;

            this.LongRunningTaskManagerService.downloadResult(taskInstance.status.id);
        }

        //public downloadGeneratingCount(): number {
        //    var cnt = $.grep(this.getTaskInstances(), (el, index) => {
        //        return el.isDownloadGenerating();
        //    }).length;

        //    return cnt;
        //}

        //public downloadGeneratedCount(): number {
        //    var cnt = $.grep(this.getTaskInstances(), (el, index) => {
        //        return el.isDownloadGenerated();
        //    }).length;

        //    return cnt;
        //}

        //public downloadGenerateFailedCount(): number {
        //    var cnt = $.grep(this.getTaskInstances(), (el, index) => {
        //        return el.isDownloadGenerationFailed();
        //    }).length;

        //    return cnt;
        //}

        //public uploadingCount(): number {
        //    var cnt = $.grep(this.getTaskInstances(), (el, index) => {
        //        return el.isUploading();
        //    }).length;

        //    return cnt;
        //}

        public inProgressCount(): number {               
            var cnt = $.grep(this.getTaskInstances(), (el, index) => {
                return !(el.status.taskInstanceStatus == LongRunningTaskInstanceStatusEnum.Failed || el.status.taskInstanceStatus == LongRunningTaskInstanceStatusEnum.Finished);
            }).length;

            return cnt;
        }
        public completedCount(): number {
            var cnt = $.grep(this.getTaskInstances(), (el, index) => {
                return el.status.taskInstanceStatus == LongRunningTaskInstanceStatusEnum.Failed || el.status.taskInstanceStatus == LongRunningTaskInstanceStatusEnum.Finished;
            }).length;

            return cnt;
        }
    }    


    // register controller
    angular.module('Sustainalytics.LRTM').controller('LongRunningTaskManagerController', LongRunningTaskManagerController);
    //angular.module('myModule', ['angular-flexslider']);
} 